package com.woniuxy.woniuhis.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Emp;
import com.woniuxy.woniuhis.exception.EmpServiceException;
import com.woniuxy.woniuhis.mapper.EmpMapper;
import com.woniuxy.woniuhis.service.EmpService;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;


    /**
     * 添加员工信息
     */
    @Override
    public void addEmp(Emp emp) {
        try {
            String uuid = UUIDUtil.uuid32();
            emp.setEmpId(uuid);
            emp.setEmpHiredate(new Date());
            empMapper.insertEmp(emp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");
        }
    }

    /**
     * 更改员工信息
     */
    @Override
    public void modifyEmpByEmpId(Emp emp) {
        try {
            empMapper.updateEmpByEmpId(emp);

        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");

        }

    }

    /**
     * 删除员工信息
     */
    @Override
    public void delEmpByempId(String empId) {
        try {
            if (empId != null && empId != "") {
                empMapper.deleteEmpByempId(empId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");
        }

    }

    /**
     * 	根据条件查询员工信息
     */
    @Override
    public List<Emp> findEmpByCondition(Emp empCondition, Integer pageIndex, Integer pagesize) {
        try {
            if (pageIndex <= 0) {
                pageIndex = 1;
            }
            if (pagesize == 0 || pagesize == null) {
                pagesize = 5;
            }
            return empMapper.selectEmpByCondition(empCondition);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");
        }
    }


    /**
     * 	根据员工id查询员工信息   员工详情页
     */
    @Override
    public Emp findEmpByempId(String empId) {

        try {
            Emp findEmp = empMapper.selectEmpByempId(empId);
            return findEmp;
        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");
        }
    }
    
    @Override
    public PageInfo<Emp> findEmps(Emp emp, Integer pageIndex, Integer pageSize) {
        try {
        	PageHelper.startPage(pageIndex, pageSize);
            List<Emp> list = empMapper.selectEmps(emp);
            return new PageInfo<>(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");
        }
    }

	// 根据用户名查找用户
	@Override
	public Emp findEmpByEmpName(String empName) {
		try {
            return empMapper.selectEmpByEmpName(empName);

        } catch (Exception e) {
            e.printStackTrace();
            throw new EmpServiceException("系统维护中");
        }
	}
}
